package com.example.schoolmgt.client;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.net.URISyntaxException;

@Component
public class Client {
    private static final Logger logger = LoggerFactory.getLogger(Client.class);
    @Resource
    private RestTemplate restTemplate;

    public long createTeacher() throws URISyntaxException {
        URI uri = new URI("http://192.168.254.151:30820/teacher/teacher");
        ResponseEntity<String> exchange = restTemplate.exchange(uri, HttpMethod.POST, null, String.class);
        if (exchange.getStatusCode().is2xxSuccessful()) {
            JSONObject resBody = JSON.parseObject(exchange.getBody());
            return resBody.getLongValue("id");
        }
        return -1;
    }

    public long createCourse(long school, long teacher) throws URISyntaxException {
        URI uri = new URI("http://192.168.254.151:30810/course/scheduleCourse?schoolId="+school+"&teacherId="+teacher);
        ResponseEntity<String> exchange = restTemplate.exchange(uri, HttpMethod.POST, null, String.class);
        if (exchange.getStatusCode().is2xxSuccessful()) {
            JSONObject resBody = JSON.parseObject(exchange.getBody());
            return resBody.getLongValue("id");
        }
        return -1;
    }
}
